#include "chooselevelscene.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <mypushbutton.h>
#include <QDebug>
#include <QTimer>
#include <QLabel>
#include <QFont>
#include <QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡场景
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/resource/Coin0001.png"));
    this->setWindowTitle("选择关卡");
    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);
    //创建开始菜单
    QMenu *startMenu=bar->addMenu("开始");
    //创建退出功能
    QAction *quitAction=startMenu->addAction("退出");
    //实现退出功能
    connect(quitAction,&QAction::triggered,this,[=]()
    {
        this->close();
    });
    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/resource/BackButton.png",":/resource/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    //返回音效
    QSound *backSound=new QSound(":/resource/BackButtonSound.wav",this);
    //实现点击返回
    connect(backBtn,&MyPushButton::clicked,[=]()
    {
        backSound->play();
        QTimer::singleShot(300,this,[=]()
        {
           emit this->chooseSceneBack();
        });
    });
    //选择音效
    QSound *chooseSound=new QSound(":/resource/TapButtonSound.wav",this);
    //创建关卡选择按钮
    for(int i=0;i<20;i++)
    {
        MyPushButton *menuBtn=new MyPushButton(":/resource/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->resize(80,80);
        menuBtn->move(this->width()*0.5-2*menuBtn->width()+(i%4)*menuBtn->width(),180+i/4*menuBtn->height());
        //通过QLabel设置数字
        QLabel *menulabel=new QLabel;
        QFont labelFont("黑体",18,QFont::Bold);
        menulabel->setParent(this);
        menulabel->setFont(labelFont);
        menulabel->setFixedSize(menuBtn->width(),menuBtn->height());
        menulabel->move(this->width()*0.5-2*menuBtn->width()+(i%4)*menuBtn->width(),180+i/4*menuBtn->height());
        menulabel->setText(QString::number(i+1));
        menulabel->setAlignment(Qt::AlignCenter);
        //让鼠标穿透label
        menulabel->setAttribute(Qt::WA_TransparentForMouseEvents);
        //监听每个按钮的点击事件后进入游戏场景
        connect(menuBtn,&MyPushButton::clicked,this,[=]()
        {
            chooseSound->play();
            playScene=new PlayScene(i+1);
            playScene->setGeometry(this->geometry());
            this->hide();
            playScene->show();
            connect(playScene,&PlayScene::chooseSceneBack,this,[=]()
            {
               this->setGeometry(playScene->geometry());
               playScene->hide();
               this->show();
               delete playScene;
               playScene=NULL;
            });
        });
    }



}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    //加载标题
    pix.load(":/resource/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载背景
    pix.load(":/resource/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
